<?php
namespace app\index\controller;
use app\common\Pinyin;
use app\index\controller\Base;
use app\model\Activity;
use app\model\ActivityCompany;
use app\model\ActivityUser;
use app\model\Job;
use app\model\Link;
use app\model\Places;
use app\model\School;
use app\model\SchoolStudentListen;
use app\model\User;
use app\model\UserCompany;
use app\model\UserResume;
use app\model\UserResumeCertificate;
use app\model\UserResumeEdu;
use app\model\UserResumeExp;
use think\Cache;

class Index extends  Base
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>';
    }




    public function config(){
        //var_dump(config("web.aaa"));
        $arr=config("web");
        return $this->apiJosn($arr);

    }




    /**
     * 注册
     */
    public function register(){
        $params=request()->param();

        if($params['mobile']==false){
            return $this->apiJosn([],201,'mobile没有定义');
        }
        if($params['password']==false){
            return $this->apiJosn([],201,'password没有定义');
        }

        $num=$this->get_mobile_code($params['mobile']);
        if($num==false){
            return $this->apiJosn([],201,'手机验证码已经失效');
        }
        if($params['code']!=$num){
            return $this->apiJosn([],201,'手机验证码错误');
        }

        $UserModel=new User();

        $if_exit=$UserModel->if_exit_by_mobile($params['mobile']);
        if($if_exit==true){
            return $this->apiJosn([],201,'手机号已经存在');
        }

        $r=$UserModel->add_user($params);
        if($r){
            //var_dump($r);
            $token=$this->get_token(['user_id'=>$r]);
            return $this->apiJosn(['token'=>$token],200,'注册成功');
        }else{
            return $this->apiJosn([],201,'注册失败');
        }


    }

    /**
     * 发送手机验证码
     * @return string
     */
    public function send_mobile_code(){
        $params=request()->param();
        if($params['mobile']==false){
            return $this->apiJosn([],201,'mobile不能为空');
        }
        $r=$this->send_mobie_code_act($params['mobile']);
        if($r['code']==0){
            return $this->apiJosn(['num'=>$r['num']],200,$r['tip']);
        }else{
            return $this->apiJosn(['num'=>$r['num']],201,$r['tip']);
        }
    }


    private function send_mobie_code_act($mobile){
        $rand_num=rand(1111,9999);
        Cache::set($mobile,$rand_num,2*60*60);

        require_once '../application/common/chuanglan253-php-master/demo/API/ChuanglanSmsHelper/ChuanglanSmsApi.php';
        $clapi  = new \ChuanglanSmsApi();

        //设置您要发送的内容：其中“【】”中括号为运营商签名符号，多签名内容前置添加提交
        $result = $clapi->sendSMS($mobile,'【校脉通】短信验证码是'.$rand_num);
        if(!is_null(json_decode($result))){
            $output=json_decode($result,true);
            if(isset($output['code'])  && $output['code']=='0'){
                //echo $result;
                return ['code'=>0,'tip'=>"发送成功",'num'=>''];
            }else{
                return ['code'=>1,'tip'=>"发送失败".$output['errorMsg'],'num'=>''];
                //echo $result;
            }
        }else{
            //echo $result;
            return ['code'=>1,'tip'=>"发送失败",'num'=>''];
        }


        //return ['code'=>0,'tip'=>"发送成功".$output['errorMsg'],'num'=>''];
    }

    private function get_mobile_code($mobile){
        $num=Cache::get($mobile);
        return $num;
    }

    /**
     * 登陆
     */
    public function login(){
        $params=request()->param();
        if($params['mobile']==false){
            return $this->apiJosn([],201,'手机号不能为空');
        }
        if($params['password']==false){
            return $this->apiJosn([],201,'密码不能为空');
        }
        $UserModel=new User();
        $u_arr=$UserModel->get_user_info_by_mobile($params['mobile']);
        //var_dump($u_arr);
        if($u_arr==false){
            return $this->apiJosn([],201,'手机号不存在');
        }
       // var_dump($params);
        //var_dump(md5($params['password']));
        if($u_arr['password']==md5($params['password'])){
           $token= $this->get_token(['user_id'=>$u_arr['id']]);
            return $this->apiJosn(['token'=>$token],200,'登陆成功');
        }else{
            return $this->apiJosn([],201,'密码错误');
        }
    }

    //插件图像上传
    public function uploadify(){
        // 1.获取上传的文件数据
        $file = request()->file("file");
        //var_dump($file);
        // 判断是否存在文件上传对象
        if ($file) {
            // 2.移动到指定目录下
            // ROOT_PATH 框架应用根目录
            // DS 当前系统的目录分隔符
            $files  = $file->move(ROOT_PATH."public".DS."uploads");
            //dump($files);
            if($file!=false){
                //var_dump($files->getSaveName());
                $file_name=str_replace("\\",'/',$files->getSaveName());
                $data['img_url']="/uploads/".$file_name;
                $data['img_url']=pic_host($data['img_url']);
                return $this->apiJosn($data,200);
            }else{
                return $this->apiJosn([],201,'上传失败');
            }

            // 获取文件信息
            //dump($files->getSaveName());
            // 获取文件后缀名
            //dump($files->getExtension());

        }else{
            return $this->apiJosn([],201,'input中的name=file没有定义');
        }


    }

    /**
     * 返回工作列表
     */
    function get_job_list(){
        $params=request()->param();
        if($params['page']==false){
            $params['page']=1;
        }
        $JobModel=new Job();
        $list=$JobModel->get_all_job($params['page'],$params);
        return $this->apiJosn($list,200);
    }

    /**
     * 返回工作信息
     */
    function get_job_info(){
        $params=request()->param();
        if($params['id']==false){
            return $this->apiJosn([],201,'信息ID不能为空');
        }
        $JobModel=new Job();
        $info=$JobModel->get_job_info_by_id($params['id']);
        return $this->apiJosn($info,200);

    }

    /**
     * 返回所有用户的简历
     */
    function get_user_resume_list(){
        $params=request()->param();
        if($params['page']==false){
            $params['page']=1;
        }
        $UserResume= new UserResume();
        $list=$UserResume->get_user_resume($params['page'],$params);
        return $this->apiJosn($list);
    }

    /**
     * 根据简历主键ID，返回对应的简历详情
     */
    function get_user_resume_info(){
        $params=request()->param();
        if($params['id']==false){
            return $this->apiJosn([],201,'简历ID主键不能为空');
        }



        $UserResume=new UserResume();

        $user_id=$UserResume->get_user_id_by_id($params['id']);
        if($user_id==false){
            return $this->apiJosn([],201,'对应的简历已经删除');
        }


        $UserResumeEdu=new UserResumeEdu();
        $UserResumeExp=new UserResumeExp();
        $UserResumeCertificate=new UserResumeCertificate();
        $User=new User();
        $info['base']=$UserResume->get_info($user_id);
        $info['edu']=$UserResumeEdu->get_arrs_by_user_id($user_id);
        $info['exp']=$UserResumeExp->get_arrs_by_user_id($user_id);
        $info['cer']=$UserResumeCertificate->get_arrs_by_user_id($user_id);
        $info['expect']=$User->get_exp($user_id);
        return $this->apiJosn($info,200);


    }

    /**
     * 返回首字母城市列表
     */
    function get_area(){
        $PlacesModel=new Places();
        $arr=config("web.place_pingyin_f");
        foreach($arr as $k=>$v){
            $f=$v;
            $arr=$PlacesModel->get_list_by_city_pingyin($v);
            $tem_arr[]=['f'=>$f,'f_city'=>$arr];
        }
        return $this->apiJosn($tem_arr,200);

    }


    function test(){
        $Pinyin=new Pinyin();
        $Places=new Places();
        $o=$Places->select();
        $arrs=object2array($o);
        foreach($arrs as $k=>$v){
            $pingyin=$Pinyin->getPinyin($v['cname'], $charset='utf-8');
            $pingyin=strtoupper(substr($pingyin,0,1));
            $Places->where(['id'=>$v['id']])->update(['pingyin'=>$pingyin]);
        }
    }

    /**
     * 根据公司ID，返回他的公司信息
     */
    function get_company_info_by_id(){
        $params=request()->param();
        if($params['id']==false){
            return $this->apiJosn([],201,'公司信息ID的主键不能为空');
        }
        $UserCompany=new UserCompany();
        $user_id=$UserCompany->get_user_id_by_id($params['id']);
        $c_info=$UserCompany->get_user_company_by_id($params['id']);
        return $this->apiJosn($c_info,200);
    }

    /**
     * 通过公司主键ID，返回他所有的发布的工作列表
     */
    function get_company_job_list_by_company_id(){
        $params=request()->param();
        if($params['id']==false){
            return $this->apiJosn([],201,'公司信息ID的主键不能为空');
        }
        $Job=new Job();
        $UserCompany=new UserCompany();
        $company_user_id=$UserCompany->get_user_id_by_id($params['id']);
        $list=$Job->get_all_job_list_by_user_id($company_user_id);
        return $this->apiJosn($list);

    }

    /**
     * 取得所有的活动列表
     */
    function get_activity_list(){
        $params=request()->param();
        if(if_null($params['status'])){
            return $this->apiJosn([],'201','status不能为空');
        }
        $Activity=new Activity();
        $list=$Activity->get_all_activity_list($params);
        return $this->apiJosn($list);
    }

    /**
     * 根据活动主键返回活动详情信息
     */
    function get_activity_info_by_id(){

        //var_dump();
        if($this->request->param('id')==false){
            return $this->apiJosn([],201,'活动ID不能为空');
        }
        $Activity=new Activity();
        $info=$Activity->get_info($this->request->param('id'));
        $ActivityCompany=new ActivityCompany();
        $ActivityUser=new ActivityUser();
        $info['ac_company_join']=$ActivityCompany->get_ac_join_company_num($this->request->param('id'));
        $info['ac_user_join']=$ActivityUser->get_ac_join_user($this->request->param('id'));
        return $this->apiJosn($info);
    }

    /**
     * 通过活动ID，返回已经参加招聘活动的公司列表
     */
    function get_company_list_by_activity_id(){
        if($this->request->param('id')==false){
            return $this->apiJosn([],201,'活动ID不能为空');
        }
        $Activity=new ActivityCompany();
        $list=$Activity->get_join_comapny_list_by_activity_id($this->request->param('id'));
        return $this->apiJosn($list);
    }


    /**
     * 根据活动ID,返回已经报名参加的用户列表
     */
    function get_join_user_by_activity_id(){
        if($this->request->param('id')==false){
            return $this->apiJosn([],201,'活动ID不能为空');
        }
        $ActivityUser=new ActivityUser();
        $list=$ActivityUser->get_join_user_by_activity_id($this->request->param('id'));
        return $this->apiJosn($list);
    }


    /**
     * 返回省
     */
    function get_province(){
        $PlacesModel=new Places();
        $arrs=$PlacesModel->get_province();
        return $this->apiJosn($arrs);
    }

    /**
     * 根据省ID，返回城市ID
     */
    function get_city_by_p_id(){
        $params=request()->param();
        if($params['p_id']==false){
           return $this->apiJosn([],201,'p_id不能为空');
        }
        $PlacesModel=new Places();
        $arrs=$PlacesModel->get_city_by_p_id($params['p_id']);
        return $this->apiJosn($arrs);
    }

    /**
     * 根据手机号返回token便于调试
     */
    function get_auth_by_mobile(){
        $params=request()->param();
        if($params['mobile']==false){
            return $this->apiJosn([],201,'手机号不能为空');
        }

        $UserModel=new User();
        $u_arr=$UserModel->get_user_info_by_mobile($params['mobile']);
        //var_dump($u_arr);
        if($u_arr==false){
            return $this->apiJosn([],201,'手机号不存在');
        }
        // var_dump($params);
        //var_dump(md5($params['password']));

        $token= $this->get_token(['user_id'=>$u_arr['id']]);
        return $this->apiJosn(['token'=>$token],200);


    }

    /**
     * 返回所有的公司列表
     */
    function get_all_company(){
        $UserCompany=new UserCompany();
        $list=$UserCompany->get_all_company();
        return $this->apiJosn($list);
    }

    function get_area_name_by_id(){
        $params=request()->param();
        if($params['id']==false){
            return $this->apiJosn([],201,'id不能为空');
        }
        $Places=new Places();
        $name=$Places->get_name_by_id($params['id']);
        return $this->apiJosn(['name'=>$name]);
    }

    /**
     * 取得同级地方列表，传同一级地方ID
     */
    function get_equle_by_id(){
        $params=request()->param();
        if($params['id']==false){
            return $this->apiJosn([],201,'id不能为空');
        }
        $Places=new Places();
        $list=$Places->get_equle_list($params['id']);
        return $this->apiJosn($list);
    }

    /**
     * 根据用户ID,返回他的所在的公司信息
     */
    function get_company_info_by_user_id(){
        $params=request()->param();
        if($params['user_id']==false){
            return $this->apiJosn([],201,'用户ID不能为空');
        }
        $UserCompany=new UserCompany();
        $info=$UserCompany->get_user_company($params['user_id']);
        return $this->apiJosn($info);
    }


    function get_school(){
        $params=request()->param();

        $School=new School();
        $list=$School->get_school_by_params($params);
        return $this->apiJosn($list);
    }


    /**
     * 返回 所有学校列表
     */
    function get_all_school(){
        $School=new School();
        $list=$School->get_all_school();
        return $this->apiJosn($list);
    }

    /**
     * 根据学校ID,返回他的详情
     */
    function get_school_info_by_id(){
        $params=request()->param();
        if($params['id']==false){
            return $this->apiJosn([],201,'学校ID不能为空');
        }
        $School=new School();
        $info=$School->get_school_info_by_id($params['id']);
        return $this->apiJosn($info);
    }

    /**
     * 返回学校中的打听列表
     */
    function get_school_listen(){
        $params=request()->param();
        if($params['school_id']==false){
            return $this->apiJosn([],201,'学校ID不能为空');
        }
        if($params['cur_page']==false){
            return $this->apiJosn([],201,'第几页不能为空');
        }
        $SchoolStudentListen=new SchoolStudentListen();

        $list=$SchoolStudentListen->get_page_list($params['school_id'],$params['cur_page']);
        return $this->apiJosn($list);

    }

    /**
     * 返回 首页导航图
     */
    function get_banner(){
        $Link=new Link();
        $list=$Link->get_all_link();
        return $this->apiJosn($list);
    }








}
